package com.bw.paper.mvp.presenter;

import com.bw.paper.App;
import com.bw.paper.mvp.base.model.BaseEntity;
import com.bw.paper.mvp.base.presenter.BasePresenter;
import com.bw.paper.mvp.contract.UserDetailsContract;
import com.bw.paper.mvp.model.entity.UpdateUserEntity;
import com.bw.paper.mvp.model.entity.UploadFileEntity;
import com.bw.paper.mvp.model.entity.UserEntity;
import com.bw.paper.utils.LogUtils;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class UserDetailsPresenter extends BasePresenter<UserDetailsContract.IUserDetailsModel, UserDetailsContract.IUserDetailsView> {

    private String file;

    @Inject
    public UserDetailsPresenter(UserDetailsContract.IUserDetailsModel iUserDetailsModel, UserDetailsContract.IUserDetailsView iUserDetailsView) {
        super(iUserDetailsModel, iUserDetailsView);
    }

    public void requestUserDetails(Map<String,Object>map){
        if (map == null)
            return;
        m.requestUser(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public void requestUpdateUser(){
        Map<String,Object>map = new HashMap<>();
        UserEntity entity = App.getInstance().getDaoSession().queryBuilder(UserEntity.class)
                .list().get(0);
        map.put("user_id",entity.user_id);
        map.put("user_img",file.substring(file.lastIndexOf("/")+1));
        map.put("user_integral",entity.user_integral);
        map.put("user_name",entity.user_name);
        map.put("user_phone",entity.user_phone);
        map.put("user_pwd",entity.user_pwd);
        map.put("user_sex",entity.user_sex);
        LogUtils.show(entity.toString());
        requestUpdateUser(map);
    }

    public void requestUpdateUser(Map<String,Object>map){
        if (map == null)
            return;
        m.updateUser(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public void uploadHead(String file){
        if (file.isEmpty())
            return;
        this.file = file;
        Map<String,Object>map = new HashMap<>();
        map.put("file",file);
        m.uploadHead(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new UploadOb(this));
    }

    @Override
    public void onNext(@NonNull BaseEntity entity) {
        if (entity instanceof UpdateUserEntity){
            LogUtils.show("UserDetailsPresenter:UpdateUserEntity");
//            //更新数据成功
            UserEntity userEntity = App.getInstance().getDaoSession().queryBuilder(UserEntity.class)
                    .list().get(0);
            Map<String,Object>map = new HashMap<>();
            map.put("user_id",userEntity.user_id);
            requestUserDetails(map);
            return;
        }
        v.refreshView(entity);
    }

    private static class UploadOb implements Observer<UploadFileEntity>{

        private UserDetailsPresenter presenter;
        public UploadOb(UserDetailsPresenter presenter){
            this.presenter = presenter;
        }

        @Override
        public void onSubscribe(@NonNull Disposable d) {
            this.presenter.onSubscribe(d);
        }

        @Override
        public void onNext(@NonNull UploadFileEntity uploadFileEntity) {
            presenter.requestUpdateUser();
        }

        @Override
        public void onError(@NonNull Throwable e) {
            this.presenter.onError(e);
        }

        @Override
        public void onComplete() {
            this.presenter.onComplete();
        }
    }

}
